﻿// 以下代码由 Microsoft Visual Studio 2005 生成。
// 测试所有者应该检查每个测试的有效性。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;

namespace TestMiniAccess
{
    using Ruandao.MiniAccess;
    using Ruandao.MiniAccess.SqlServer;
    using System.Data;
    using Ruandao.MiniAccess.OrMap;

    [TestClass()]
    public class TableMapTest
    {


        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestMethod()]
        public void ConstructorTest()
        {
            string tableName = "ForeignKeyTable";
            string connString = ConfigurationManager.ConnectionStrings["TestMiniAccessConnection"].ConnectionString;
            Database database = new SqlDatabase(connString);

            TableMap target = new TableMap(tableName, database);

            Assert.AreEqual(tableName, target.TableName);

            // PrimaryKeyColumns
            Assert.AreEqual(3, target.PrimaryKeyColumns.Count);
            Assert.AreEqual("CompositeKey1", target.PrimaryKeyColumns[0].ColumnName);
            Assert.AreEqual("CompositeKey2", target.PrimaryKeyColumns[1].ColumnName);
            Assert.AreEqual("CompositeKey3", target.PrimaryKeyColumns[2].ColumnName);

            Assert.AreEqual(false, target.IsPrimaryKey("Field1"));

            // Identity
            Assert.AreEqual(false, target.IsIdentity("CompositeKey1"));
            Assert.AreEqual(null, target.IdentityColumn);

            // ForeighKey
            Assert.AreEqual(true, target.IsForeignKey("PrimaryKeyId"));
            Assert.AreEqual(false, target.IsForeignKey("TableId"));
            Assert.AreEqual(false, target.IsForeignKey("Field1"));


            Assert.AreEqual(8, target.ColumnMaps.Count);
        }

        [TestMethod()]
        public void GetTableMapTest()
        {
            string tableName = "ForeignKeyTable";
            string connString = ConfigurationManager.ConnectionStrings["TestMiniAccessConnection"].ConnectionString;
            Database database = new SqlDatabase(connString);

            TableMap actual = new TableMap(tableName, database);
            actual.SetDefaultTableMap(database, CommandType.StoredProcedure, true);
            TableMap_Accessor actual_accessor = new TableMap_Accessor(new PrivateObject(actual));

            Assert.AreEqual(8, actual_accessor.ColumnMaps.Count, "Ruandao.MiniDac.SqlServer.OrMapHelper.GetTableMap 未返回所需的值。");

            Assert.AreEqual("Insert_ForeignKeyTable",actual.InsertCommand);
            Assert.AreEqual("Delete_ForeignKeyTable",actual.DeleteCommand);
            Assert.AreEqual("Delete_ForeignKeyTable_By_Conditions",actual.DeleteByConditionsCommand);
            Assert.AreEqual("Update_ForeignKeyTable",actual.UpdateCommand);
            Assert.AreEqual("Select_ForeignKeyTable_By_PrimaryKey",actual.FetchCommand);
            Assert.AreEqual("Select_ForeignKeyTable_By_Conditions",actual.SearchCommand);

            // ColumnMaps[0]: ColumnMapType.PrimaryKeyMap, "CompositeKey1", "CompositeKey1", true, true
            Assert.AreEqual(ColumnMapType.PrimaryKeyMap,actual.ColumnMaps[0].ColumnMapType);
            Assert.AreEqual("CompositeKey1",actual.ColumnMaps[0].ColumnName);
            Assert.AreEqual("CompositeKey1",actual.ColumnMaps[0].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[0].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[0].IsInsert);

            // ColumnMaps[1]: ColumnMapType.PrimaryKeyMap, "CompositeKey2", "CompositeKey2", true, true
            Assert.AreEqual(ColumnMapType.PrimaryKeyMap,actual.ColumnMaps[1].ColumnMapType);
            Assert.AreEqual("CompositeKey2",actual.ColumnMaps[1].ColumnName);
            Assert.AreEqual("CompositeKey2",actual.ColumnMaps[1].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[1].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[1].IsInsert);

            // ColumnMaps[2]: ColumnMapType.PrimaryKeyMap, "CompositeKey3", "CompositeKey3", true, true
            Assert.AreEqual(ColumnMapType.PrimaryKeyMap,actual.ColumnMaps[2].ColumnMapType);
            Assert.AreEqual("CompositeKey3",actual.ColumnMaps[2].ColumnName);
            Assert.AreEqual("CompositeKey3",actual.ColumnMaps[2].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[2].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[2].IsInsert);

            // ColumnMaps[3]: ColumnMapType.ManyToOneMap, "PrimaryKeyId", "PrimaryKeyId", true, true
            Assert.AreEqual(ColumnMapType.ManyToOneMap,actual.ColumnMaps[3].ColumnMapType);
            Assert.AreEqual("PrimaryKeyId",actual.ColumnMaps[3].ColumnName);
            Assert.AreEqual("PrimaryKeyId",actual.ColumnMaps[3].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[3].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[3].IsInsert);

            // ColumnMaps[4]: ColumnMapType.ManyToOneMap, "TableId", "TableId", true, true
            Assert.AreEqual(ColumnMapType.PropertyMap, actual.ColumnMaps[4].ColumnMapType);
            Assert.AreEqual("TableId",actual.ColumnMaps[4].ColumnName);
            Assert.AreEqual("TableId",actual.ColumnMaps[4].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[4].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[4].IsInsert);

            // ColumnMaps[5]: ColumnMapType.PropertyMap, "GuidField", "GuidField", false, true
            Assert.AreEqual(ColumnMapType.PropertyMap,actual.ColumnMaps[5].ColumnMapType);
            Assert.AreEqual("GuidField",actual.ColumnMaps[5].ColumnName);
            Assert.AreEqual("GuidField",actual.ColumnMaps[5].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[5].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[5].IsInsert);

            // ColumnMaps[6]: ColumnMapType.PropertyMap, "Field1", "Field1", true, true
            Assert.AreEqual(ColumnMapType.PropertyMap,actual.ColumnMaps[6].ColumnMapType);
            Assert.AreEqual("Field1",actual.ColumnMaps[6].ColumnName);
            Assert.AreEqual("Field1",actual.ColumnMaps[6].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[6].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[6].IsInsert);

            // ColumnMaps[7]: ColumnMapType.PropertyMap, "Field2", "Field2", true, true
            Assert.AreEqual(ColumnMapType.PropertyMap,actual.ColumnMaps[7].ColumnMapType);
            Assert.AreEqual("Field2",actual.ColumnMaps[7].ColumnName);
            Assert.AreEqual("Field2",actual.ColumnMaps[7].PropertyName);
            Assert.AreEqual(true,actual.ColumnMaps[7].IsUpdate);
            Assert.AreEqual(true,actual.ColumnMaps[7].IsInsert);
        }
    }


}
